<script>
{literal}
 $( function() { $('a[rel*=facebox]').facebox(); } );
{/literal}
</script>

<a href="#newproject" rel="facebox">+ Add Project</a>
<br style="clear: both;" />

<div id="projects" class="sidebar">
    <ul>
    {foreach from=$projects item=p}
        {if $p.ID != $active_project}
            <li><a href="index.php?p=projects&amp;projectID={$p.ID}">{$p.name} ({$p.count}/{$p.count_inc_folders})</a></li>
        {else}
            <li class="active"><a href="index.php?p=projects&amp;projectID={$p.ID}">{$p.name} ({$p.count}/{$p.count_inc_folders})</a></li>
            <ul>
            <li><a href="#newfolder" rel="facebox">+ Add Folder</a></li>
            {foreach from=$folders item=f}
            {if $f.ID != $active_folder}
                <li><a href="index.php?p=projects&amp;projectID={$p.ID}&amp;folderID={$f.ID}">{$f.name} ({$f.count})</a></li>
            {else}
                <li class="active"><a href="index.php?p=projects&amp;projectID={$p.ID}&amp;folderID={$f.ID}">{$f.name} ({$f.count})</a></li>
            {/if}
            {/foreach}
            </ul>
        {/if}
        <hr />
    {/foreach}
    </ul>
</div> 

{if $active_folder == 0}
<a href="#newfield" rel="facebox">+ Add Project Field</a>
{else}
<a href="#newfield" rel="facebox">+ Add Folder Field</a>
{/if}
<table class="results" style="font-size: smaller;">
<tr><th>Entry</th><th>Fields</th></tr>
{section name=results loop=$results}
    <tr link="index.php?p=view&ID={$results[results].ID}" class="{cycle values="rowalt1,rowalt2"}">
        <td style="font-size: x-small;" valign="top">
        <strong>{$results[results].title}</strong><hr style="margin: 0;" />
        {$results[results].author}<br />
        <em>{$results[results].journal} ({$results[results].year})</em></td>
        <td style="font-size: x-small;" valign="top">
            <a href="#editfields{$results[results].ID}" rel="facebox">Edit Fields</a><br />
            {foreach from=$fields item=f}
            <strong>{$f.name}:&nbsp;</strong><br /> {$results[results].fields[$f.ID]}<br />
            {/foreach}
        </td>
    </tr>
{/section}
{if count($results) == 0}
<tr><td colspan="10"><strong>No Entries Found</strong></td></tr>
{/if}
</table>


<br style="clear: both;" />

{foreach from=$results item=r}
<div id="editfields{$r.ID}" style="display: none;">
    <form method="post" action="index.php">
        <table border="0" cellpadding="0" cellspacing="0">
        {foreach from=$fields item=f}
        <tr>
            <td align="right"><label style="font-size: small;" for="field_{$f.ID}_{$r.ID}">{$f.name}</label>&nbsp;</td>
            <td><input type="text" name="field[{$f.ID}]" id="field_{$f.ID}_{$r.ID}" value="{$r.fields[$f.ID]}" /></td>
        </tr>
        {/foreach}
        </table>
        <input type="hidden" name="p" value="projects" />
        <input type="hidden" name="projectID" value="{$active_project}" />
        <input type="hidden" name="folderID" value="{$active_folder}" />
        <input type="hidden" name="entryID" value="{$r.ID}" />
        <input type="hidden" name="save_fields" value="1" />
        <input type="submit" value="Save Fields" />
    </form>
</div>
{/foreach}

<div id="newproject" style="display: none;">
    <form method="post" action="index.php">
        <label for="add_name">Project Name</label> <input type="text" name="add_name" id="add_name" />
        <input type="hidden" name="p" value="projects" />
        <input type="submit" value="Create Project" />
    </form>
</div>
<div id="newfolder" style="display: none;">
    <form method="post" action="index.php">
        <label for="add_folder_name">Folder Name</label> <input type="text" name="add_folder_name" id="add_folder_name" />
        <input type="hidden" name="p" value="projects" />
        <input type="hidden" name="projectID" value="{$active_project}" />
        <input type="submit" value="Create Folder" />
    </form>
</div>
<div id="newfield" style="display: none;">
    <form method="post" action="index.php">
    {if $active_folder == 0}<span style="font-size: x-small;">Fields added a project are used in all project folders as well.</span><br />{/if}
        <label for="add_field_name">Field Name</label> <input type="text" name="add_field_name" id="add_field_name" />
        <input type="hidden" name="p" value="projects" />
        <input type="hidden" name="projectID" value="{$active_project}" />
        <input type="hidden" name="folderID" value="{$active_folder}" />
        <input type="submit" value="Create Field" />
    </form>
</div>
